package cn.tansanqinger.dao.sql.hy_menu;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IHyMenuSqlDao;
import cn.tansanqinger.pojo.HyMenu;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;

@Repository
public class HyMenuSqlDaoImpl {
	
	@Autowired
	private IHyMenuSqlDao service;
	
	/**
	 * 查询一级目录全部数据
	 * @return
	 */
	public List<HyMenu> selectFirstMenus() {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		queryWrapper.lambda().eq(HyMenu::getHparentId, 0);
		return service.selectList(queryWrapper);
	}
	
	/**
	 * 分页查询
	 * @param page
	 * @param limit
	 * @return
	 */
	@SuppressWarnings("unused")
	public List<HyMenu> selectHy_Menus(int page, int limit, String keywords, int hparentId) {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		if (hparentId!=-1) {
			queryWrapper.and(wrapper -> wrapper.eq("hparent_id", hparentId).or().eq("hmenu_id", hparentId));
		}
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("hmenu_name", keywords).or().
				like("hmenu_sn", keywords).or().like("hmenu_url", keywords));
		} 
		Page<HyMenu> pg = new Page<HyMenu>(page,limit);
		IPage<HyMenu> iPage = service.selectPage(pg, queryWrapper);
		return iPage.getRecords();
	}
	
	/**
	 * 查询数据总条数
	 * @return
	 */
	public int selectHy_MenusLength(int page, int limit, String keywords, int hparentId) {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		if (hparentId!=-1) {
			queryWrapper.and(wrapper -> wrapper.eq("hparent_id", hparentId).or().eq("hmenu_id", hparentId));
		}
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("hmenu_name", keywords).or().
				like("hmenu_sn", keywords).or().like("hmenu_url", keywords));
		} 
		return service.selectCount(queryWrapper);
	}
	
	/**
	 * 查询菜单序列号的最大值
	 * @return
	 */
	public int selectSnMax() {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		queryWrapper.orderByDesc("hmenu_sn").last("limit 1");
		return service.selectOne(queryWrapper).getHmenuSn();
	}
	
	/**
	 * 通过菜单名称查询相关信息
	 * @param HmenuName
	 * @return
	 */
	public HyMenu selectHmenuId (Object HmenuName) {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		queryWrapper.lambda().eq(HyMenu::getHmenuName, HmenuName);
		return service.selectOne(queryWrapper);
	}
	
	/**
	 * 查询该菜单下面的编号最大值
	 * @param hparent_id
	 * @return
	 */
	public HyMenu serialNumber(int hparent_id) {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		queryWrapper.lambda().eq(HyMenu::getHparentId, hparent_id);
		queryWrapper.orderByDesc("hmenu_order").last("limit 1");
		return service.selectOne(queryWrapper);
	}
	
	/**
	 * 添加数据
	 * @param hyMenu
	 * @return
	 */
	public int insertMenu(HyMenu hyMenu) {
		return service.insert(hyMenu);
	}
	
	/**
	 * 通过menuId查询数据
	 * @param menuId
	 * @return
	 */
	public HyMenu selectMenuIdOne(int menuId) {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		queryWrapper.lambda().eq(HyMenu::getHmenuId, menuId);
		return service.selectOne(queryWrapper);
	}
	
	/**
	 * 通过id进行数据修改
	 * @param hyMenu
	 * @return
	 */
	public int updataMenu(HyMenu hyMenu) {
		QueryWrapper<HyMenu> queryWrapper = new QueryWrapper<HyMenu>();
		queryWrapper.lambda().eq(HyMenu::getHmenuId, hyMenu.getHmenuId());
		return service.update(hyMenu, queryWrapper);
	}
	
	/**
	 * 通过hmenuId修改菜单信息
	 * @param hmenuId
	 * @param isOnSale
	 * @return
	 */
	public int updateMenuStatus(int hmenuId, int isOnSale) {
		HyMenu hyMenu = new HyMenu();
		hyMenu.setIsOnSale(isOnSale);
		hyMenu.setHmenuCtime(DateUtil.getTime());
		return service.update(hyMenu, new QueryWrapper<HyMenu>().eq("hmenu_id", hmenuId));
	}
	
	/**
	 * 通过hmenuId删除数据
	 * @param hmenuId
	 * @return
	 */
	public int deleteMenu(int hmenuId) {
		return service.deleteById(hmenuId);
	}
	
}
